home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / spiele / gnuchess / src / amiga / gettimeofday.c < prev    next >
C/C++ Source or Header  |  1995-08-13  |  2KB  |  112 lines

  1. /**
  2. ***  simple gettimeofday() replacement; no timezone handling
  3. ***
  4. ***  Jochen Wiedmann, 14-Feb-95
  5. ***
  6. ***  This is in the public domain, use it as you want.
  7. **/
  8.  
  9.  
  10. /****************************************************************
  11.     This file uses the auto initialization possibilities of
  12.     Dice, gcc and SAS/C, respectively.
  13.  
  14.     Dice does this by using the keywords __autoinit and
  15.     __autoexit, SAS uses names beginning with _STI or
  16.     _STD, respectively. gcc uses the asm() instruction,
  17.     to emulate C++ constructors and destructors.
  18. ****************************************************************/
  19.  
  20.  
  21. #if defined(__SASC)
  22. #define __autoinit
  23. #define __autoexit
  24. #elif defined(__GNUC__)
  25. #define __autoinit
  26. #define __autoexit
  27. asm ("  .text;  .stabs \"___CTOR_LIST__\",22,0,0,__STIInitGetTimeOfDay");
  28. asm ("  .text;  .stabs \"___DTOR_LIST__\",22,0,0,__STDTermGetTimeOfDay");
  29. #elif defined(_DCC)
  30. extern void _AutoFail0(void);
  31. #else
  32. #error "Don't know how to handle your compiler."
  33. #endif
  34.  
  35. #include <stdlib.h>
  36. #include <errno.h>
  37.  
  38. #include <exec/types.h>
  39. #include <exec/io.h>
  40. #include <devices/timer.h>
  41. #include <proto/exec.h>
  42.  
  43.  
  44. STATIC struct MsgPort *port     = NULL;
  45. STATIC struct timerequest *req  = NULL;
  46.  
  47. STATIC __autoinit LONG _STIInitGetTimeOfDay(VOID)
  48.  
  49. { if ((port = CreateMsgPort()))
  50.   { if ((req = CreateIORequest(port, sizeof(*req))))
  51.     { if (!(OpenDevice((STRPTR) "timer.device", UNIT_MICROHZ,
  52.                (struct IORequest *) req, 0)))
  53.       { return(FALSE);
  54.       }
  55.       DeleteIORequest(req);
  56.       req = NULL;
  57.     }
  58.     DeleteMsgPort(port);
  59.     port = NULL;
  60.   }
  61. #if defined(__GNUC__)
  62.   abort();
  63. #elif defined(_DCC)
  64.   _AutoFail0();
  65. #endif
  66.   return(TRUE);
  67. }
  68.  
  69.  
  70. STATIC __autoexit VOID _STDTermGetTimeOfDay(VOID)
  71.  
  72. { if (req)
  73.   { CloseDevice((struct IORequest *) req);
  74.     DeleteIORequest(req);
  75.     req = NULL;
  76.   }
  77.   if (port)
  78.   { DeleteMsgPort(port);
  79.     port = NULL;
  80.   }
  81. }
  82.  
  83.  
  84. struct timezone;
  85. int gettimeofday(struct timeval *tv, struct timezone *tz)
  86.  
  87. { int error;
  88.  
  89.   req->tr_node.io_Command = TR_GETSYSTIME;
  90.   error = DoIO((struct IORequest *) req);
  91.   *tv = req->tr_time;
  92.   if (error)
  93.   { errno = ENOMEM;
  94.     return(-1);
  95.   }
  96.   return(0);
  97. }
  98.  
  99. int setsystime(struct timeval *tv, struct timezone *tz)
  100.  
  101. { int error;
  102.  
  103.   req->tr_time = *tv;
  104.   req->tr_node.io_Command = TR_SETSYSTIME;
  105.   error = DoIO((struct IORequest *) req);
  106.   if (error)
  107.   { errno = ENOMEM;
  108.     return(-1);
  109.   }
  110.   return(0);
  111. }
  112.